package com.fyfeng.fcanvas;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.fyfeng.android.tools.annotation.AnnotationUtils;
import com.fyfeng.android.tools.annotation.ClickMethod;
import com.fyfeng.android.tools.annotation.LongClickMethod;

public class GraffitiBoardUI extends Activity {

	private static final String tag = GraffitiBoardUI.class.getSimpleName();

	private FCanvas mPaintView;// 涂画的View层

	private Bitmap mNoteBitmap;// 上次塗畫BITMAP

	private RelativeLayout mBackground;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.fcontentui);

		AnnotationUtils.init(this);

		// 塗寫區域
		mBackground = (RelativeLayout) this.findViewById(R.id.view);

		this.setOriginalPicture(mBackground);// 設置底圖

	}

	/**
	 * 初始化底層塗畫
	 * 
	 * @param view
	 */
	private void setOriginalPicture(RelativeLayout view) {
		AssetManager am = this.getResources().getAssets();

		Bitmap resource_pic = null;
		InputStream input = null;
		try {
			input = am.open("images" + File.separator + FFileUtils.mOriginalFile);
			resource_pic = BitmapFactory.decodeStream(input);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != input) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		if (null != resource_pic) {
			view.setBackgroundDrawable(new BitmapDrawable(resource_pic));
		}
	}

	/**
	 * 长按界面显示操作栏
	 * 
	 * @param v
	 */
	@LongClickMethod(id = R.id.view)
	public void onLongClickPaintView(View v) {
		Toast.makeText(this, "长按界面.", Toast.LENGTH_SHORT).show();
		View view = this.findViewById(R.id.buttonarea);
		view.setVisibility(View.VISIBLE);

		// 初始化绘图层
		if (null == this.mPaintView) {

			this.mPaintView = new FCanvas(this, this.mBackground.getWidth(), this.mBackground.getHeight());
			this.mBackground.addView(mPaintView);
		}

	}

	/**
	 * 切换到橡皮擦模式
	 * 
	 * @param v
	 */
	@ClickMethod(id = R.id.button_erase)
	public void onClickErase(View v) {

		Button button_erase = (Button) v;

		View button_paint = this.findViewById(R.id.button_paint);
		View button_clear = this.findViewById(R.id.button_clear);
		View button_save = this.findViewById(R.id.button_save);

		if (FMode.ERASE != this.mPaintView.getState()) {
			this.mPaintView.toErase();

			button_erase.setText(R.string.finish);
			button_paint.setEnabled(false);
			button_clear.setEnabled(false);
			button_save.setEnabled(false);
		} else if (FMode.ERASE == this.mPaintView.getState()) {
			this.mPaintView.toPaint();
			button_erase.setText(R.string.erase);
			button_paint.setEnabled(true);
			button_clear.setEnabled(true);
			button_save.setEnabled(true);
		}
	}

	@ClickMethod(id = R.id.button_paint)
	public void onClickPaint(View v) {
		this.mPaintView.toPaint();
	}

	@ClickMethod(id = R.id.button_clear)
	public void onClickClear(View view) {
		if (null == this.mPaintView) {
			return;
		}

		this.mPaintView.clear();
	}

	@ClickMethod(id = R.id.button_undo)
	public void onClickUndo(View v) {
		this.mPaintView.undo();
	}

	@ClickMethod(id = R.id.button_save)
	public void onClickSave(View v) {

		if (null == this.mPaintView) {
			return;
		}

		this.mPaintView.toRead();

		View view = this.findViewById(R.id.buttonarea);
		view.setVisibility(View.GONE);

		Bitmap newBitmap = this.mPaintView.getBitmap();

		if (null == newBitmap) {
			Log.i(tag, "无涂写内容");
			return;
		}

		boolean result = FFileUtils.saveNoteFile(newBitmap, FFileUtils.mNoteFile);
		if (result) {
			Log.i(tag, "Note保存成功.");
		} else {
			Log.e(tag, "Note保存失败.");
		}

	}

}
